/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoSelecaoLegenda2.java
 *
 * Created on 30/08/2011, 12:43:16
 */
package savet.gui.dialogos;

import java.util.List;
import savet.entidades.Ementa;
import savet.entidades.atributos.Legenda;
import savet.gui.controllers.EmentarioController;
import savet.gui.modelos.ListaLegendaModel;
import savet.gui.util.EmentarioPanelUtilController;
import savet.util.ListLegendas;

/**
 *
 * @author Rafael
 */
public class DialogoSelecaoLegenda2 extends javax.swing.JDialog implements EmentarioPanelUtilController {

    private int tipoLegenda;
//    private List<Legenda> legendasSelecionadas = new LinkedList<Legenda>();
    private EmentarioController controller = new EmentarioController();
    private LegendaListListener legendaListListener;
    private ListLegendas listLegendas = ListLegendas.getInstance();
    private Class atributoRelacionado;
    private Ementa ementaSelecionada;

    /**
     * Cria instancia de DialogoSelecaoLegenda
     * @param parent
     * @param modal
     */
    public DialogoSelecaoLegenda2(java.awt.Frame parent, boolean modal) {
        this(parent, modal, null, null);
    }

    /**
     * Cria instancia de DialogoSelecaoLegenda
     *
     * @param parent
     * @param modal
     * @param tipoLegenda indica legendas que serao carregadas para o dialogo. Default {@link Legenda#LEGENDA_AUTUACAO}.
     */
    public DialogoSelecaoLegenda2(java.awt.Frame parent, boolean modal, LegendaListListener legendaListListener, Class atributoRelacionado) {
        super(parent, modal);
        controller.setPanelUtilController(this);
        this.legendaListListener = legendaListListener;
        initComponents();
        this.atributoRelacionado = atributoRelacionado;
        System.out.println("DialogoSelecaoLegenda2");
        carregarLegendas(atributoRelacionado);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        listaLegendaModelAutuacao = new savet.gui.modelos.ListaLegendaModel();
        listaLegendaModelOrientacao = new savet.gui.modelos.ListaLegendaModel();
        panelSelecao = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        codigoEmenta = new javax.swing.JFormattedTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        notaRelatorio = new javax.swing.JTextArea();
        saveButton1 = new org.openswing.swing.client.SaveButton();
        jRadioButtonA = new javax.swing.JRadioButton();
        jRadioButtonO = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDescricaoLegenda = new javax.swing.JTextArea();
        panelSelecao1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        deleteAutuacao = new org.openswing.swing.client.DeleteButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        deleteOrientacao = new org.openswing.swing.client.DeleteButton();
        ementarioPanel1 = new savet.gui.forms.EmentarioPanel(controller);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelSelecao.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Criar legendas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setText("Código da Ementa:");

        jLabel2.setText("Descrição da legenda:");

        jLabel3.setText("Nota para relatório:");

        codigoEmenta.setEditable(false);

        notaRelatorio.setColumns(20);
        notaRelatorio.setFont(new java.awt.Font("Arial", 0, 11));
        notaRelatorio.setLineWrap(true);
        notaRelatorio.setRows(15);
        jScrollPane2.setViewportView(notaRelatorio);

        saveButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonA);
        jRadioButtonA.setSelected(true);
        jRadioButtonA.setText("Autuação");

        buttonGroup1.add(jRadioButtonO);
        jRadioButtonO.setText("Orientação");

        jLabel6.setText("Tipo de Ementas:");

        jTextAreaDescricaoLegenda.setColumns(20);
        jTextAreaDescricaoLegenda.setFont(new java.awt.Font("Arial", 0, 11));
        jTextAreaDescricaoLegenda.setLineWrap(true);
        jTextAreaDescricaoLegenda.setRows(15);
        jScrollPane1.setViewportView(jTextAreaDescricaoLegenda);

        javax.swing.GroupLayout panelSelecaoLayout = new javax.swing.GroupLayout(panelSelecao);
        panelSelecao.setLayout(panelSelecaoLayout);
        panelSelecaoLayout.setHorizontalGroup(
            panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSelecaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSelecaoLayout.createSequentialGroup()
                        .addGroup(panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelSelecaoLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(codigoEmenta, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                            .addGroup(panelSelecaoLayout.createSequentialGroup()
                                .addComponent(saveButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButtonA)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonO)
                        .addGap(101, 101, 101))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelSelecaoLayout.setVerticalGroup(
            panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSelecaoLayout.createSequentialGroup()
                .addGroup(panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jRadioButtonA)
                        .addComponent(jRadioButtonO))
                    .addComponent(saveButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelSelecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codigoEmenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelSelecao1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Legendas Existentes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jList1.setModel(listaLegendaModelAutuacao);
        jList1.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP);
        jList1.setVisibleRowCount(50);
        jScrollPane3.setViewportView(jList1);

        deleteAutuacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAutuacaoActionPerformed(evt);
            }
        });

        jList2.setModel(listaLegendaModelOrientacao);
        jList2.setVisibleRowCount(50);
        jScrollPane4.setViewportView(jList2);

        jLabel4.setText("Legendas de Autuação:");

        jLabel5.setText("Legendas de Orientação:");

        deleteOrientacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteOrientacaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelSelecao1Layout = new javax.swing.GroupLayout(panelSelecao1);
        panelSelecao1.setLayout(panelSelecao1Layout);
        panelSelecao1Layout.setHorizontalGroup(
            panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSelecao1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(deleteAutuacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(deleteOrientacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelSelecao1Layout.setVerticalGroup(
            panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSelecao1Layout.createSequentialGroup()
                .addGroup(panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelSelecao1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSelecao1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteAutuacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSelecao1Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteOrientacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        ementarioPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Ementário", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelSelecao1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelSelecao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ementarioPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ementarioPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 611, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelSelecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panelSelecao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButton1ActionPerformed
        Legenda legenda = new Legenda();
        legenda.setEmenta(ementaSelecionada);
        legenda.setDescricao(jTextAreaDescricaoLegenda.getText());
        legenda.setNota(notaRelatorio.getText());
        if (buttonGroup1.isSelected(jRadioButtonA.getModel())) {
            legenda.setTipo(Legenda.LEGENDA_AUTUACAO);
            listaLegendaModelAutuacao.addLegenda(legenda);
//            legendasSelecionadas.add(legenda);
            legendaListListener.addLegend(legenda);
        } else {
            legenda.setTipo(Legenda.LEGENDA_ORIENTACAO);
            listaLegendaModelOrientacao.addLegenda(legenda);
//            legendasSelecionadas.add(legenda);
            legendaListListener.addLegend(legenda);
        }

    }//GEN-LAST:event_saveButton1ActionPerformed

    private void carregarLegendas(Class atributoRelacionado) {
        System.out.println("Tamanho da lista:" + listLegendas.getList().size());
        for (Legenda legenda : listLegendas.getList()) {
            if (legenda.getAtributoRelacionado() == atributoRelacionado) {
                if (legenda.getTipo() == Legenda.LEGENDA_AUTUACAO) {
                    listaLegendaModelAutuacao.addLegenda(legenda);
                } else {
                    listaLegendaModelOrientacao.addLegenda(legenda);
                }
            }
        }
    }

    private void deleteAutuacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteAutuacaoActionPerformed

        List<Legenda> legendas = listaLegendaModelAutuacao.getLegendasSelecionadas();

        for (Legenda legenda : legendas) {
            listaLegendaModelAutuacao.removeLegenda(legenda);
            listLegendas.removeLegenda(legenda);
        }

}//GEN-LAST:event_deleteAutuacaoActionPerformed

    private void deleteOrientacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteOrientacaoActionPerformed
        List<Legenda> legendas = listaLegendaModelOrientacao.getLegendasSelecionadas();

        for (Legenda legenda : legendas) {
            listaLegendaModelOrientacao.removeLegenda(legenda);
            listLegendas.removeLegenda(legenda);
        }
}//GEN-LAST:event_deleteOrientacaoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DialogoSelecaoLegenda2 dialog = new DialogoSelecaoLegenda2(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFormattedTextField codigoEmenta;
    private org.openswing.swing.client.DeleteButton deleteAutuacao;
    private org.openswing.swing.client.DeleteButton deleteOrientacao;
    private savet.gui.forms.EmentarioPanel ementarioPanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JRadioButton jRadioButtonA;
    private javax.swing.JRadioButton jRadioButtonO;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextAreaDescricaoLegenda;
    private savet.gui.modelos.ListaLegendaModel listaLegendaModelAutuacao;
    private savet.gui.modelos.ListaLegendaModel listaLegendaModelOrientacao;
    private javax.swing.JTextArea notaRelatorio;
    private javax.swing.JPanel panelSelecao;
    private javax.swing.JPanel panelSelecao1;
    private org.openswing.swing.client.SaveButton saveButton1;
    // End of variables declaration//GEN-END:variables

//    public List<Legenda> getLegendasSelecionadas() {
//        return legendasSelecionadas;
//    }
    public void atualizarComponentes(Ementa ementa) {
        ementaSelecionada = ementa;
        codigoEmenta.setText(ementa.getEmenta());
        jTextAreaDescricaoLegenda.setText(ementa.getDescricao());
        notaRelatorio.setText(ementa.getNota());
    }

    public ListaLegendaModel getListaLegendaModelAutuacao() {
        return listaLegendaModelAutuacao;
    }

    public ListaLegendaModel getListaLegendaModelOrientacao() {
        return listaLegendaModelOrientacao;
    }

    public LegendaListListener getLegendaListListener() {
        return legendaListListener;
    }

    public void setLegendaListListener(LegendaListListener legendaListListener) {
        this.legendaListListener = legendaListListener;
    }

    public Class getAtributoRelacionado() {
        return atributoRelacionado;
    }

    public void setAtributoRelacionado(Class atributoRelacionado) {
        this.atributoRelacionado = atributoRelacionado;
    }
}
